package com.hhh.petadopt.serviceImpl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.hhh.petadopt.mapper.AdminUserMapper;
import com.hhh.petadopt.pojo.User;
import com.hhh.petadopt.pojo.UserPage;
import com.hhh.petadopt.pojo.UserVo;
import com.hhh.petadopt.service.AdminUserService;
import com.hhh.petadopt.utils.Page;
/**
 * 后台用户信息管理
 * @author 杜镇声声
 *
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

	@Autowired
	private AdminUserMapper adminUsermapper;
	
	//将用户信息按分页返回
	@Override
	public Page<User> selectAllUser(UserPage vo) {
		//将分页的内容进行封装
		Page<User> page = new Page<User>();
		//设置每页显示的条数
		page.setSize(10);
		vo.setSize(10);
		if(null != vo) {
			if(null != vo.getPage()) {
				//设置当前页
				page.setPage(vo.getPage());
				//设置起始页
				vo.setStartRow((vo.getPage()-1)*vo.getSize());
			}
			//查询总条数,将结果进行封装
			page.setTotal(adminUsermapper.selectCount());
			//封装当前页显示的内容
			page.setRows(adminUsermapper.selectAllUsers(vo));
		}
		return page;
	}

	//搜索条件分页
	@Override
	public Page<User> selectUserByVo(UserVo vo) {
		//创建page对象
		Page<User> page = new Page<User>();
		//设置当前页显示的条数
		page.setSize(10);
		vo.setSize(10);
		if(null != vo) {
			if(vo.getPage() != null) {
				
				page.setPage(vo.getPage());
				//设置起始页
				vo.setStartRow((vo.getPage()-1)*vo.getSize());
			}
			//对查询条件进行封装
			if(null != vo.getUsername() && !"".equals(vo.getUsername().trim())) {
				vo.setUsername(vo.getUsername().trim());
			}
			if(null != vo.getAddress() && !"".equals(vo.getAddress().trim())) {
				vo.setAddress(vo.getAddress().trim());
			}
			//查询符合条件的总条数,将数据进行封装
			page.setTotal(adminUsermapper.selectTotalByVo(vo));
			//查询当前页要显示的内容,进行封装
			page.setRows(adminUsermapper.selectUserByVo(vo));
		}
		return page;
	}

	//用户信息回显
	@Override
	public User selectUserById(Integer uid) {
		User user = adminUsermapper.selectUserByid(uid);
		//将用户密码转换成******
		user.setPassword("******");
		return user;
	}

	//添加用户
	@Override
	public void addusers(User user) {
		//将密码进行md5加密
		String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
		user.setPassword(password);
		//对创建日期和更改日期进行设置
		user.setCreated(new Date());
		user.setUpdated(new Date());
		adminUsermapper.addUsers(user);
		
	}

	//删除用户,单个删除
	@Override
	public void deleteUser(Integer uid) {
		//删除
		adminUsermapper.deleteUser(uid);
		
	}

	//批量删除用户
	@Override
	public void deletedUserByIds(Integer[] ids) {
		adminUsermapper.deleteUserByIds(ids);
		
	}

	//修改用户信息
	@Override
	public void updateUserr(User user) {
		//封装数据
		user.setCreated(new Date());
		user.setUpdated(new Date());
		adminUsermapper.updateUserr(user);
		
	}

}
